// pages/selfCenter/selfCenter.js

const {
  login,
  zhuce,
  amount
} = require('../../utils/http')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    code: '',
    token: '',
    uId: "",
    amount: {}
  },
  getUserInfo(){
    const that = this
    wx.getUserInfo({
      success: function (res) {
        var userInfo = res.userInfo
        console.log(res)
        that.setData({
          userInfo
        })
      }
    })
    console.log(123)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this
    
    // wx.getUserInfo({
    //   withCredentials: 'false',
    //   lang: 'zh_CN',
    //   timeout:10000,
    //   success: (result) => {
    //     console.log(result)
    //   },
    //   fail: () => {},
    //   complete: () => {}
    // });
      
    wx.showModal({
      title: '提示',
      content: '需要获取授权信息',
      success: function (res) {
        if (res.confirm) {
          that.getUserInfo()
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
    // 查看用户是否注册
    wx.login({
      success: function (res) {
        const { code } = res
        //判断用户是否已经注册
        zhuce(code).then(resZ => {
          // console.log(resZ)
          console.log('用户注册')
        })
      }
    })
    wx.login({
      success: function (res1) {
        const { code } = res1
        login(code).then(res2 => {
          // console.log(res2)

          wx.setStorageSync('token', res2.data.token);

          amount(res2.data.token).then(res3 => {
            that.setData({
              token: res2.data.token,
              amount: res3.data,
              uId: res2.data.uid
            })
            // console.log(that.data.uId)
          })
        })
      }
    })
    

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})